//封装Ajax请求
$.extend({
    ajaxDirect:function(url,type,data,success,error){
        if(type.toLowerCase()=='post'||type.toLowerCase()=='put'){
            data = pack(data)
        }
		var base='http://120.55.77.106:3000'
        $.ajax({
            type: type,
            url:  base+url,
            contentType:'application/json',
            data:data,
            beforeSend: function (XMLHttpRequest) {
                XMLHttpRequest.setRequestHeader("authorization", localStorage.getItem('token'))
            },
            success: function(data){
				if(data.code!=200){
					layer.msg(data.error)
				}
                success(data)
            },
            error: function(data){
                error(data)
				console.log(data)
				if(data.responseJSON.error=='无效的令牌'){
					layer.msg('登录失效，请重新登录')
					setTimeout(()=>{
						location.href='admin_login.html'
					},500)
				}else{
					layer.msg(data.responseJSON.error)
				}
				
            },
            dataType: "json"
        })
    },
	ajaxapi:function(){
		return 'http://120.55.77.106:3000'
	}
})

//对象序列化
function pack(data) {
    return JSON.stringify(data)
}

function unpack(data) {
    return JSON.parse(data)
}